var postData = require('../../data/posts-data.js')
var app = getApp()
var api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseURL: 'https://shipin.lekuaiwang.com'
  },
  onReady: function () {
    this.dialog = this.selectComponent('#dialog')
    console.log(this.dialog.data.isShow)
    if (this.dialog.data.isShow == false) {
      this.dialog.showDialog()
    }

    // wx.setStorageSync('show', true)
  },
  bindGetUserInfo: function (e) {
    console.log(this.dialog.data.isShow)
    app.login()
  },
  confirmEvent: function () {
    this.dialog.hideDialog()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var _this = this

    // 查看是否授权
    wx.getSetting({
      success(res) {
        console.log('===')
        console.log(res)
        console.log(123)

        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          _this.dialog.setData({
            isShow: false
          })
          wx.getUserInfo({
            success(res) {
              console.log(res.userInfo)
            }
          })
        }
      }
    })
    // 文章数据本地化

    this.setData({
      post_content: postData.postList
    })
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/index/index?openid=ouP0X41h2y_10uqCM_KZ7GyoB8c8',
      data: {},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        _this.setData({
          listsData: res.data.data.data
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
    // var postId = postData.postId
    // console.log(postId)

    // 获取与设置文章收藏标识
    // var postsCollected = wx.getStorageSync('posts_Collected')
    // var postCollected = postsCollected[postId]
    // if (!postsCollected) {
    //   var postsCollected = {}
    // }
    // if (postCollected) {
    //   this.setData({
    //     collected: postCollected
    //   })
    // } else {
    //   postsCollected[postId] = false
    //   wx.setStorageSync('posts_Collected', postsCollected)
    // }

  },
  /**
   * 视频列表点击事件
   */
  onPostTap(event) {
    var id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: 'post-detail/post-detail?id=' + id
    })
    console.log(event.currentTarget.dataset.id)
    // console.log(this.data.post_content[event.currentTarget.dataset.postid].collected)
  },
  onCollectTap: function (event) {
    // var zan = this.data.zan
    // this.setData({
    //   zan: !zan
    // })
    console.log(event.currentTarget.dataset)
    var postId = event.currentTarget.dataset.postid
    var postIndex = event.currentTarget.dataset.index
    var openid = wx.getStorageSync('openid')
    var listsData = this.data.listsData
    var _this = this
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/resource/collection?id=' + postId + '&openid=' + openid,
      data: {},
      method: 'GET',
      success: function (res) {
        // success
        console.log(res)
        listsData[postIndex].is_collection = !listsData[postIndex].is_collection
        if (res.data.code == 200) {
          _this.setData({
            listsData: listsData
          })
          // console.log(_this.data.listsData[postIndex].is_collection)
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  // showModal: function (postsCollected, currentCollect) {
  //   var that = this
  //   wx.showModal({
  //     title: '收藏文章',
  //     content: currentCollect ? '收藏该文章？' : '取消收藏该文章？',
  //     showCancel: true,
  //     cancelText: '取消',
  //     cancelColor: '#333',
  //     confirmText: '确认',
  //     confirmColor: '#405f80',
  //     success: function (res) {
  //       if (res.confirm) {
  //         // 更新缓存中收藏的状态
  //         wx.setStorageSync('posts_Collected', postsCollected)
  //         // 更新当前文章的收藏状态
  //         // that.setData({
  //         //   collected: currentCollect
  //         // })
  //         // console.log(that.data.postId)
  //         const cId = that.data.postId
  //         console.log(cId)
  //         that.setData({
  //           'post_content[0].collected': currentCollect
  //         })
  //       }
  //     }
  //   })
  // },
})